package com.zt.questionnaire.common.utils;

import com.zt.questionnaire.common.exception.CustomException;
import com.zt.questionnaire.common.msg.HErrorCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;

/**
 * @Description: 数据校验
 * @Author: FUNNYWUS
 * @Date: 2021/6/7 11:03
 */
public class AssertUtils {

    private static final Logger log = LoggerFactory.getLogger(AssertUtils.class);

    /**
     * string is blank
     *
     * @param str     string
     * @param message errorMsg
     * @throws CustomException
     */
    public static void isBlank(String str, String message) throws CustomException {
        if (StringUtils.isBlank(str)) {
            newThrow(HErrorCode.PARAM_ERROR, message);
        }
    }

    /**
     * string is not blank
     *
     * @param str     string
     * @param message errorMsg
     * @throws CustomException
     */
    public static void isNotBlank(String str, String message) throws CustomException {
        if (StringUtils.isNotBlank(str)) {
            newThrow(HErrorCode.PARAM_ERROR, message);
        }
    }

    /**
     * object is (null or size = 0)
     *
     * @param object  object
     * @param message errorMsg
     * @throws CustomException
     */
    public static void isEmpty(Object object, String message) throws CustomException {
        if (ObjectUtils.isEmpty(object)) {
            newThrow(HErrorCode.PARAM_ERROR, message);
        }
    }

    /**
     * object is not (null or size = 0)
     *
     * @param object  object
     * @param message errorMsg
     * @throws CustomException
     */
    public static void isNotEmpty(Object object, String message) throws CustomException {
        if (object != null) {
            newThrow(HErrorCode.PARAM_ERROR, message);
        }
    }

    /**
     * object is null
     *
     * @param object  object
     * @param message errorMsg
     * @throws CustomException
     */
    public static void isNull(Object object, String message) throws CustomException {
        if (object == null) {
            newThrow(HErrorCode.PARAM_ERROR, message);
        }
    }

    /**
     * object is not null
     *
     * @param object  object
     * @param code    HErrorCode
     * @param message errorMsg
     * @throws CustomException
     */
    public static void isNotNull(Object object, int code, String message) throws CustomException {
        if (object != null) {
            log.error(message, object);
            newThrow(code, message);
        }
    }

    public static void isNotNull(Object object, String message) throws CustomException {
        if (object != null) {
            log.error(message, object);
            newThrow(message);
        }
    }

    /**
     * expression is true
     *
     * @param expression object
     * @param code       HErrorCode
     * @param message    errorMsg
     * @throws CustomException
     */
    public static void isTrue(boolean expression, int code, String message) throws CustomException {
        if (expression) {
            newThrow(code, message);
        }
    }

    /**
     * expression is true
     *
     * @param object  object
     * @param code    HErrorCode
     * @param message errorMsg
     * @throws CustomException
     */
    public static void isTrue(boolean expression, String message) throws CustomException {
        isTrue(expression, HErrorCode.FAIL, message);
    }

    /**
     * expression is false
     *
     * @param expression expression
     * @param code       HErrorCode
     * @param message    errorMsg
     * @throws CustomException
     */
    public static void isFalse(boolean expression, String message) throws CustomException {
        if (!expression) {
            newThrow(HErrorCode.FAIL, message);
        }
    }

    /**
     * expression is false
     *
     * @param expression expression
     * @param code       HErrorCode
     * @param message    errorMsg
     * @throws CustomException
     */
    public static void isFalse(boolean expression, int code, String message) throws CustomException {
        if (!expression) {
            newThrow(code, message);
        }
    }

    /**
     * db update fail
     *
     * @param expression expression
     * @throws CustomException
     */
    public static void dbUpdateFail(boolean expression) throws CustomException {
        isFalse(expression, HErrorCode.DB_UPDATE_FAIL, "更新失败");
    }

    /**
     * db save fail
     *
     * @param expression expression
     * @param code       HErrorCode
     * @param message    errorMsg
     * @throws CustomException
     */
    public static void dbSaveFail(boolean expression) throws CustomException {
        isFalse(expression, HErrorCode.DB_SAVE_FAIL, "保存失败");
    }

    /**
     * db delete fail
     *
     * @param expression expression
     * @throws CustomException
     */
    public static void dbDeleteFail(boolean expression) throws CustomException {
        isFalse(expression, HErrorCode.DB_DELETE_FAIL, "删除失败");
    }

    /**
     * db save fail, obj not null
     *
     * @param expression expression
     * @param code       HErrorCode
     * @param message    errorMsg
     * @throws CustomException
     */
    public static void dbExist(Object obj) throws CustomException {
        dbExist(obj, "添加的数据已经存在！");
    }

    /**
     * db save fail, obj not null
     *
     * @param expression expression
     * @param code       HErrorCode
     * @param message    errorMsg
     * @throws CustomException
     */
    public static void dbExist(Object obj, String message) throws CustomException {
        isNotNull(obj, HErrorCode.DB_EXIST, message);
    }

    /**
     * db save fail, obj not null
     *
     * @param expression expression
     * @param code       HErrorCode
     * @param message    errorMsg
     * @throws CustomException
     */
    public static void dbExist(boolean is, String message) throws CustomException {
        isTrue(is, HErrorCode.DB_EXIST, message);
    }

    /**
     * 抛出一个自定义异常
     *
     * @param code    HErrorCode
     * @param message errorMsg
     */
    public static void newThrow(int code, String message) {
        throw new CustomException(code, message);
    }

    /**
     * 抛出一个自定义异常
     */
    public static void newThrow() {
        throw new CustomException(HErrorCode.FAIL, "error");
    }

    /**
     * 抛出一个自定义异常
     *
     * @param message errorMsg
     */
    public static void newThrow(String message) {
        throw new CustomException(HErrorCode.FAIL, message);
    }

    /**
     * 数据库记录不存在
     *
     * @param obj obj
     */
    public static void dbNotExist(Object obj) {
        dbNotExist(obj, "此条记录不存在！");
    }

    /**
     * 数据库记录不存在
     *
     * @param obj     obj
     * @param message errorMsg
     */
    public static void dbNotExist(Object obj, String message) {
        isNull(obj, message);
    }
}
